package com.jsoft.controller;

import com.jsoft.entity.Apply;
import com.jsoft.service.ApplyService;
import com.jsoft.util.Result;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Apply)表控制层
 *
 * @author makejava
 * @since 2025-03-14 23:30:21
 */
@RestController
@RequestMapping("apply")
public class ApplyController {
    /**
     * 服务对象
     */
    @Resource
    private ApplyService applyService;

    /**
     * 根据userId查询用户申请过的所有工作
     * @param userId 用户id
     * @return 查询结果
     */
    @GetMapping("getAppliedJobs")
    public Result<List<Apply>> getAppliedJobs(@RequestParam("userId") int userId) {
        List<Apply> appliedJobsByUserId = applyService.getAppliedJobsByUserId(userId);
        return Result.success(appliedJobsByUserId);
    }

    /**
     * 点击申请按钮，将该工作插入申请表
     * @param applyDTO 该职位相关信息
     * @return 插入成功
     */
    @PostMapping("/applyJob")
    public Result applyJob(@RequestBody Apply applyDTO) {
            applyService.applyJob(applyDTO);
            return Result.success();
    }
    /**
     * 分页查询
     *
     * @param apply 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Apply>> queryByPage(Apply apply, PageRequest pageRequest) {
        return ResponseEntity.ok(this.applyService.queryByPage(apply, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Apply> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.applyService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param apply 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Apply> add(Apply apply) {
        return ResponseEntity.ok(this.applyService.insert(apply));
    }

    /**
     * 编辑数据
     *
     * @param apply 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Apply> edit(Apply apply) {
        return ResponseEntity.ok(this.applyService.update(apply));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.applyService.deleteById(id));
    }

    /**
     * 申请工作
     * @param jobId
     * @return
     */
    @PostMapping("/application")
    public Result application(@Param("jobId") Integer jobId){

        applyService.application(jobId);

        return Result.success();

    }
}

